#include "headfiles.h"


using namespace std;

int main(int argc, char **argv) {
	if (argc != 2) {
		cout << "Usage: " << argv[0] << " file_name" << endl;
		return 0;
	}
	
	char file_name[256];
	sprintf(file_name, "%s", argv[1]);

	fstream af;
	af.open(file_name, fstream::in);
	if (!af.is_open()) {
		cout << "Open file " << file_name << " error!" << endl;
		return 1;
	}

	string str;
	af >> str;
	int node_num = atoi(str.c_str());
	if (node_num <= 0) {
		cout << "Number of nodes " << node_num << " is wrong!" << endl;
		return 1;
	}

	int *arr = (int *)malloc(sizeof(int) * node_num);
	if (arr == NULL) {
		cout << "Allocate array buffer error, NULL!" << endl;
		return 1;
	}

	int index = 0;
	while (af >> str) {
		if (index > node_num) {
			break;
		}

		if (str.empty()) {
			break;
		}

		int val = atoi(str.c_str());
		arr[index] = val;
		++index;
	}

	if (index != node_num) {
		cout << "Incorrect input file!" << endl;
		return 1;
	}
	af.close();

	NODE *head = createMinDepthTree(arr, node_num);
	if (head == NULL) {
		cout << "Create min depth tree error!" << endl;
		return 1;
	}

	// Print the binary tree
	printBinaryTree(head);

	return 0;	
}

